<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;

class Admin extends Base
{
    /**
     * 管理员列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function AdminList(){
        //记录数量
        $Count = Db::name('admin')->count();
        $this->assign('Count',$Count);

        //管理员信息
        $AdminList = Db::name('admin')->select();
        foreach($AdminList as $k=>$v){
            //角色信息
            $RoleInfo = Db::name('admin_role')->where(['id'=>$AdminList[$k]['role']])->find();
            $AdminList[$k]['role_name'] = $RoleInfo['name'];
        }
        $this->assign('AdminList',$AdminList);

        return $this->fetch('AdminList');
    }

    /**
     * 管理员添加
     * @return array|mixed
     */
    public function AdminAdd(){
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();

            //查看账号是否存在
            $AdminInfo = Db::name('admin')->where(['username'=>$Data['username']])->find();
            if(!empty($AdminInfo)){
                return ['status'=>0,'message'=>'该账号已存在，请重新输入'];
            }
            $Data['password'] = md5($Data['password']);
            $Data['add_time'] = time();
            //向表中添加数据
            if(Db::name('admin')->insert($Data)){
                return ['status'=>1,'message'=>'添加成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        //角色信息
        $Role = Db::name('admin_role')->select();
        $this->assign('Role',$Role);

        return $this->fetch('AdminAdd');
    }


    /**
     * 管理员编辑
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function AdminEdit(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            //向表中添加数据
            if(Db::name('admin')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        $this->assign('AdminInfo',$AdminInfo);

        //角色信息
        $Role = Db::name('admin_role')->select();
        $this->assign('Role',$Role);

        return $this->fetch('AdminEdit');
    }

    /**
     * 管理员重置密码
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function AdminPassword(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            $Save['password'] = md5(123456);
            if(Db::name('admin')->where(['id'=>Param('id')])->update($Save)){
                return ['status'=>1,'message'=>'重置成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }


    /**
     * 管理员删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function AdminDelete(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            if(Db::name('admin')->where(['id'=>Param('id')])->delete()){
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }

    /**
     * 个人资料修改
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function InfoEdit(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            //向表中添加数据
            if(Db::name('admin')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        $this->assign('AdminInfo',$AdminInfo);
        return $this->fetch('InfoEdit');
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function SavePass(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        if(IsPost()){
            //请求数据
            $Data = Param();
            if(md5($Data['old_password']) != $AdminInfo['password']){
                return ['status'=>0,'message'=>'原密码错误'];
            }
            $SaveData['password'] = md5($Data['new_password']);
            //向表中添加数据
            if(Db::name('admin')->where(['id'=>$AdminInfo['id']])->update($SaveData)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        return $this->fetch('SavePass');
    }
}